package by.demchenko.sample.webapp.page.home.teachers;

import by.demchenko.sample.datamodel.Course;
import by.demchenko.sample.datamodel.Schedule;
import by.demchenko.sample.datamodel.enums.ScheduleStatus;
import by.demchenko.sample.services.CourseService;
import by.demchenko.sample.services.ScheduleService;
import by.demchenko.sample.webapp.app.BasicAuthenticationSession;
import by.demchenko.sample.webapp.page.home.BaseLayout;
import org.apache.wicket.bean.validation.PropertyValidator;
import org.apache.wicket.datetime.StyleDateConverter;
import org.apache.wicket.datetime.markup.html.form.DateTextField;
import org.apache.wicket.extensions.yui.calendar.DatePicker;
import org.apache.wicket.markup.html.form.*;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.ResourceModel;

import javax.inject.Inject;
import java.util.Arrays;
import java.util.List;

public class CreateScheduleRecord extends BaseLayout {

    @Inject
    ScheduleService scheduleService;

    @Inject
    CourseService courseService;

    @Override
    protected void onInitialize() {
        super.onInitialize();

        add(new FeedbackPanel("feedback"));

        Schedule schedule = new Schedule();
        Form<Schedule> form = new Form<>("form", new CompoundPropertyModel<>(schedule));

        List<Course> courseList = courseService.getCourseRegistrationForTable(BasicAuthenticationSession.get().getUser());// getAll(Course_.name, true, 1, 30, false);
        final DropDownChoice<Course> tfCourse = new DropDownChoice<>(
                "course",
                new PropertyModel<>(schedule, "course"),
                courseList,
                new ChoiceRenderer<>("name", "id"));
        tfCourse.setLabel(new ResourceModel("schedule.course"));
        tfCourse.setRequired(true);
        form.add(tfCourse);

        DateTextField startDate = createDateField("startDate");
        form.add(startDate);
        DateTextField endDate = createDateField("endDate");
        form.add(endDate);

        DatePicker datePickerStart = new DatePicker()
        {
            @Override
            protected String getAdditionalJavaScript()
            {
                return "${calendar}.cfg.setProperty(\"navigator\",true,false); ${calendar}.render();";
            }
        };
        datePickerStart.setShowOnFieldClick(true);
        datePickerStart.setAutoHide(true);
        startDate.add(datePickerStart);

        DatePicker datePickerEnd = new DatePicker()
        {
            @Override
            protected String getAdditionalJavaScript()
            {
                return "${calendar}.cfg.setProperty(\"navigator\",true,false); ${calendar}.render();";
            }
        };
        datePickerEnd.setShowOnFieldClick(true);
        datePickerEnd.setAutoHide(true);
        endDate.add(datePickerEnd);

        form.add(createTextField("total"));

        TextField<?> registered = createIntField("registered");
        form.add(registered);
        registered.setVisible(false);

        final DropDownChoice<ScheduleStatus> tfStatus = new DropDownChoice<>(
                "status",
                new PropertyModel<>(schedule, "status"),
                Arrays.asList(ScheduleStatus.values()),
                new EnumChoiceRenderer<>(this));
        tfStatus.add(new PropertyValidator<>());
        tfStatus.setLabel(new ResourceModel("schedule.status"));
        form.add(tfStatus);

        form.add(new SubmitLink("save-link") {
            @Override
            public void onSubmit() {
                super.onSubmit();
                if (schedule.getEndDate().compareTo(schedule.getStartDate()) < 0) {
                    error("Дата окончания должна быть больше даты начала!");
                } else {
                    scheduleService.save(schedule);
                    setResponsePage(new TeacherProfilePage());
                }
            }

            @Override
            public void onError() {
                super.onError();
            }
        });

        form.add(new Link<Void>("back-link") {

            @Override
            public void onClick() {
                setResponsePage(new TeacherProfilePage());
            }
        });

        add(form);


    }

    private TextField<?> createTextField(String fieldName) {
        final TextField<?> tField = new TextField<>(fieldName);
        tField.add(new PropertyValidator<>());
        tField.setLabel(new ResourceModel("schedule." + fieldName));
        return tField;
    }

    private DateTextField createDateField(String fieldName) {
        final DateTextField tField = new DateTextField(fieldName, new StyleDateConverter("S-", true));
        tField.add(new PropertyValidator<>());
        tField.setLabel(new ResourceModel("schedule." + fieldName));
        return tField;
    }

    private TextField<?> createIntField(String fieldName) {
        final TextField<?> tField = new NumberTextField<Integer>(fieldName);
        tField.add(new PropertyValidator<>());
        tField.setLabel(new ResourceModel("schedule." + fieldName));
        return tField;
    }

}
